<?php

/*
 * Purpose of this file is to be included in all files of the webclient except index.php. It's use is to verify if a valid 
 * and running http session is present or, if not, start a new one using also cookie data.
 */

if (!isset ($_SESSION['userid'])){
	
	$_SESSION['remoteClient'] = $_SERVER['REMOTE_ADDR']; #Client Address
	$_SESSION['serverDir'] = getcwd(); #Server Working Directory (location of index.php)
	
	/*
	 * Check Browser Type
	 */	
	$useragent = $_SERVER['HTTP_USER_AGENT'];
	
	if (preg_match('|MSIE ([0-9].[0-9]{1,2})|',$useragent,$matched)) {
		
	    $browser_version=$matched[1];
	    $browser = 'IE';
		
	} elseif (preg_match( '|Opera ([0-9].[0-9]{1,2})|',$useragent,$matched)) {
		
	    $browser_version=$matched[1];
	    $browser = 'Opera';
		
	} elseif(preg_match('|Firefox/([0-9\.]+)|',$useragent,$matched)) {
		
	        $browser_version=$matched[1];
	        $browser = 'Firefox';
			
	} elseif(preg_match('|Safari/([0-9\.]+)|',$useragent,$matched)) {
		
	        $browser_version=$matched[1];
	        $browser = 'Safari';
			
	} else {
		
	    // browser not recognized!
	    $browser_version = 0;
	    $browser= 'other';
		
	}	
	$_SESSION['remoteBrowser'] = $browser;
	$_SESSION['remoteBrowserVersion'] = $browser_version;
	
	
	/*
	 * Check Language and Global settings
	 */
	if (!((file_exists('includes/gset.php') OR file_exists('gset.php')))){
		// redirect to installation if no settings are available
		
		Header("Location: install/index.php");
		exit(); 
			
	} else {
		// read gset.php file into session array
		
			require_once('gset.php');
			
			$_SESSION['imgstore'] = $img_store;
			$_SESSION['thbstore'] = $thb_store;
			$_SESSION['institute'] = $institute;
			$_SESSION['lang'] = $lang;
			$_SESSION['serv'] = $serv;
			$_SESSION['dbase'] = $dbase;
			$_SESSION['dbusr'] = $dbusr;
			$_SESSION['dbpwd'] = $dbpwd;
			$_SESSION['imgrep'] = $img_rep;
			$_SESSION['thbrep'] = $thb_rep;
			
	}
	
	/*
	 * Load Theme settings
	 */
	require_once('gtheme.php');
	$_SESSION['theme'] = $thdir;
	
	
	/*
	 * Load Language File
	 */
	 if ($lang == '1'){
		require('./language/german.php');
	} elseif ($lang == '2'){
		require('./language/english.php');
	} elseif ($lang == '3'){
		require('./language/french.php');
	} elseif ($lang == '4'){
		require('./language/italian.php');
	}
	
	if (!isset ($_COOKIE['DicomScienceCookie'])){

		$_SESSION['userid'] = 0;
		$_SESSION['userlevel'] = 0;
		$_SESSION['firstname'] = $nls_guest_firstname;
		$_SESSION['lastname'] = $nls_guest_lastname;
		$_SESSION['username'] = "Guest";
		
	}else{

		$cookieData = explode("&",$_COOKIE['DicomScienceCookie']);
		
		$_SESSION['userid'] = $cookieData[0];
		$_SESSION['userlevel'] = $cookieData[1];
		$_SESSION['firstname'] = $cookieData[2];
		$_SESSION['lastname'] = $cookieData[3];
		$_SESSION['username'] = $cookieData[4];
			
	}
	
}

?>